package com.situ.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebFilter("/*")
public class LoginFilter implements Filter {

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {

		HttpServletRequest req = (HttpServletRequest) request;
		String uri = req.getRequestURI();

		
		//获取登录页面和提交数据校验是否能登录的请求 直接放行
		//获取webapp中静态资源的请求 直接放行
		//以前登录过就放行
		
		Boolean isLogin = req.getSession().getAttribute("currentUser") != null;

		if (isLogin || uri.equals("/login") || uri.startsWith("/lib/") || uri.startsWith("/js/")
				|| uri.startsWith("/css/") || uri.startsWith("/images/") || uri.equals("/favicon.ico")|| uri.equals("/image")) {
			chain.doFilter(request, response);
		} else {
			HttpServletResponse resp = (HttpServletResponse) response;
			resp.sendRedirect("/login");
		}

	}

}
